<?php

	class formidable_maindataset extends formidable_mainobject {
		var $aData = FALSE;
		var $sKey = FALSE;
		var $oDataSource = FALSE;
		var $aChangedCells = array();
		var $bFloating = FALSE;

		function initFloating(&$oDataSource, $aData = array()) {
			$this->setFloating();
			$this->_initInternals($oDataSource, $aData);
		}
		
		function initAnchored(&$oDataSource, $aData, $sKey) {
			$this->setAnchored();
			$this->_initInternals($oDataSource, $aData);
			$this->sKey = $sKey;
		}

		function _initInternals(&$oDataSource, $aData) {
			$this->oForm =& $oDataSource->oForm;
			$this->aData = $aData;
			$this->oDataSource =& $oDataSource;
			$this->aChangedCells = array();
		}

		function getKey() {
			return $this->sKey;
		}

		function getData() {
			reset($this->aData);
			return $this->aData;
		}

		function setCellValue($sPath, $mValue) {
			$bSet = $this->oForm->setDeepData(
				$sPath,
				$this->aData,
				$mValue
			);
			
			if($bSet === TRUE) {
				$this->aChangedCells[$sPath] = $mValue;	
			}
		}
		
		function needsToBeWritten() {
			return count($this->aChangedCells) > 0;
		}

		function setFloating() {
			$this->bFloating = TRUE;
		}

		function setAnchored() {
			$this->bFloating = FALSE;
		}

		function isFloating() {
			return $this->bFloating;
		}

		function isAnchored() {
			return !$this->isFloating();
		}
		
		function getSignature() {
			return base64_encode($this->oForm->_getSafeLock($this->sKey) . ":" . $this->sKey);
		}

		function baseCleanBeforeSession() {
			unset($this->oDataSource);
			$this->oDataSource = FALSE;
		}

		function getDataSet() {
			return array(
				"mode" => $this->isFloating() ? "create" : "update",
				"key" => $this->getKey(),
				"data" => $this->aData,
			);
		}
	}

?>